<template>
  <div class="app-container order-class">
    <el-row style="margin-bottom:10px">
      <el-col :span="3">
        <el-input v-model="search.keyword" placeholder="Keyword" style="width:95%" />
      </el-col>
      <el-button type="primary" icon="el-icon-search" @click="gettableinto" />
      <el-button type="primary" @click="handleAddRole">Add</el-button>
    </el-row>
    <el-table
      :data="tableList"
      style="width: 100%;margin-top:30px;"
      :empty-text="empty_text"
      border
    >
      <el-table-column align="center" prop="id" label="ID" width="50">
        <template slot-scope="scope">{{ scope.row.ap_id }}</template>
      </el-table-column>
      <el-table-column align="left" label="Name">
        <template slot-scope="scope">{{ scope.row.ap_name }}</template>
      </el-table-column>
      <el-table-column align="left" label="Introduction">
        <template slot-scope="scope">{{ scope.row.ap_intro }}</template>
      </el-table-column>
      <el-table-column align="header-center" label="Width" width="80">
        <template slot-scope="scope">{{ scope.row.ap_width }}</template>
      </el-table-column>
      <el-table-column align="header-center" label="Height" width="80">
        <template slot-scope="scope">{{ scope.row.ap_height }}</template>
      </el-table-column>
      <el-table-column align="header-center" label="Enable" width="80">
        <template slot-scope="scope"><el-checkbox :value="scope.row.ap_isuse===1" @change="changeEnableStatus(scope)" /></template>
      </el-table-column>
      <el-table-column align="center" label="Action" width="100">
        <template slot-scope="scope">
          <el-row>
            <el-col :span="12">
              <el-link class="el-icon-view icon-class" @click="openorderdetailsComp(scope)" />
            </el-col>
            <el-col :span="12">
              <el-link class="el-icon-edit icon-class" @click="handleEdit(scope)" />
            </el-col>
            <!-- <el-col :span="8">
              <el-link class="el-icon-delete icon-class" @click="handleDelete(scope)" />
            </el-col> -->
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? 'Edit Ad Position' : 'New Ad Position'"
    >
      <div style="margin:0 30px">
        <el-form ref="connectForm" :model="ap_info" :rules="rules" label-width="150px" label-position="left">
          <el-form-item label="Name" prop="ap_name">
            <el-input v-model="ap_info.ap_name" placeholder="Name" />
          </el-form-item>
          <el-form-item label="Introduction" prop>
            <el-input v-model="ap_info.ap_intro" placeholder="Introduction" />
          </el-form-item>
          <el-form-item label="Width" prop>
            <el-input v-model="ap_info.ap_width" type="number" placeholder="Width" />
          </el-form-item>
          <el-form-item label="Height" prop>
            <el-input v-model="ap_info.ap_height" type="number" placeholder="Height" />
          </el-form-item>
          <el-form-item label="Enabled">
            <el-checkbox v-model="ap_info.ap_isuse" />
          </el-form-item>
        </el-form>
        <div style="text-align:right;">
          <el-button type="danger" @click="dialogVisible = false">Cancel</el-button>
          <el-button type="primary" @click="confirm">Confirm</el-button>
        </div>
      </div>
    </el-dialog>
    <el-pagination
      style="margin-top:10px;float:right"
      background
      layout="slot,prev,pager,next"
      :total="totalpage"
      :current-page="search.page"
      :page-size="search.per_page"
      @current-change="handleIndexChange"
    >
      <span class="el-pagination__total">Total {{ totalpage }}</span>
    </el-pagination>
  </div>
</template>

<script>
// import MyTest from './noets.vue'
import {
  getList,
  getDetails,
  Add,
  del
} from '@/api/advertising/index'

const defaultInfo = {
  ap_id: '',
  ap_name: '',
  ap_intro: '',
  ap_isuse: false,
  ap_width: '',
  ap_height: ''
}

export default {
  components: {
    // MyTest
  },
  data() {
    return {
      statuslist: [],
      ap_info: {
        ap_id: '',
        ap_name: '',
        ap_intro: '',
        ap_isuse: false,
        ap_width: '',
        ap_height: ''
      },
      search: {
        keyword: '',
        page: 1,
        per_page: 10
      },
      empty_text: '',
      totalpage: 0,
      tableList: [],
      dialogVisible: false,
      dialogType: 'new',
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      rules: {
        ap_name: { required: true, message: 'Please enter a Name', trigger: 'blur' }
      }
    }
  },
  watch: {
    dialogVisible(newValue, oldValue) {
      if (this.$refs['connectForm']) this.$refs['connectForm'].resetFields()

      if (!newValue) {
        this.ap_info = Object.assign({}, defaultInfo)
      }
    }
  },
  created() {
    // Mock: get all routes and roles list from server
    this.getTableData()
  },
  methods: {
    handleIndexChange(index) {
      this.search.page = index
      this.getTableData()
    },
    gettableinto() {
      this.keyword = this.keywords
      this.getTableData()
    },
    async getTableData() {
      this.empty_text = 'Loading ......'
      await getList(this.search).then(response => {
        this.tableList = response.data
        this.totalpage = response.total
        this.per_page = response.per_page
        this.current_page = response.current_page
        this.empty_text = 'Can\'t find record in table'
      }).catch(() => {
        this.tableList = []
        this.empty_text = 'Can\'t find record in table'
      })
    },
    handleAddRole() {
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      getDetails({ ap_id: scope.row.ap_id }).then(res => {
        this.ap_info = res
        this.ap_info.ap_isuse = res > 0
        this.dialogVisible = true
      })
    },
    handleDelete({ $index, row }) {
      this.$confirm('Confirm to remove the Ad Placement?', 'Warning', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      })
        .then(async() => {
          await del({ ap_id: row.ap_id })
          // baseAPI.get('user/witemplate/delete?id=' + row.id + '')
          this.tableList.splice($index, 1)
          this.getTableData()
          this.$message({
            type: 'success',
            message: 'Delete succed!'
          })
        })
        .catch(err => {
          console.error(err)
        })
    },
    openorderdetailsComp(scope) {
      this.$router.push({
        path: '/advertising/ad',
        query: { ap_id: scope.row.ap_id }
      })
    },
    changeEnableStatus(scope) {
      const data = Object.assign({}, scope.row)
      data.ap_isuse = (scope.row.ap_isuse > 0 ? 0 : 1)
      Add(data).then(() => {
        this.getTableData()
      })
    },
    async confirm() {
      this.$refs.connectForm.validate(async valid => {
        if (valid) {
          const data = Object.assign({}, this.ap_info)
          if (!this.ap_info.ap_width) {
            data.ap_width = 0
          }

          if (!this.ap_info.ap_height) {
            data.ap_height = 0
          }

          await Add(data).then(async() => {
            this.dialogVisible = false
            await this.getTableData()
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
  .icon-class {
    font-size: 20px;
  }
}
</style>

<style>
.table-info-row1 td {
      color: #03be1cef;
 }
.table-info-row2 td {
      color: #0057facb;
 }
.table-info-row3 td {
      color: #fd0000c5;
 }
.order-class .el-table .cell {
  white-space: pre-line !important;
}
</style>
